<template>
  <view>
    <u-navbar title="修改密码"> </u-navbar>
    <formz ref="formz" :inputs="inputs" :data="data"></formz>

    <bottombox>
      <u-button
        type="primary"
        :loading="loading"
        text="提交"
        shape="circle"
        @click="onedit"
      ></u-button>
    </bottombox>
  </view>
</template>

<script>
import { updateUserPwd } from "@/api/system/user.js";

export default {
  data() {
    return {
      data: {
        oldPassword: "",
        newPassword: "",
        newPassword2: "",
      },
      loading: false,
      inputs: [
        {
          label: "旧密码",
          value: "oldPassword",
          type: "password",
          required: true,
        },
        {
          label: "新密码",
          value: "newPassword",
          type: "password",
          rules: [
            {
              required: true,
              message: "请输入新密码",
              trigger: "blur",
            },
            {
              min: 6,
              max: 20,
              message: "新密码长度在6-20位",
              trigger: "blur",
            },
          ],
        },
        {
          label: "确认密码",
          value: "newPassword2",
          type: "password",
          rules: [
            {
              required: true,
              message: "请输入确认密码",
              trigger: "blur",
            },
            {
              min: 6,
              max: 20,
              message: "确认密码长度在6-20位",
              trigger: "blur",
            },
            {
              validator: (rule, value, callback) => {
                if (value !== this.$refs.formz.formData.newPassword) {
                  return callback(new Error("两次输入密码不一致"));
                } else {
                  return callback();
                }
              },
            },
          ],
        },
      ],
    };
  },
  mounted() {},
  methods: {
    async onedit() {
      let data = await this.$refs.formz.validate();
      this.loading = true;
      try {
        await updateUserPwd(data.oldPassword, data.newPassword);
        this.loading = false;
        await this.$u.modal("修改成功");
        this.$u.back();
      } catch (error) {
        this.loading = false;
      }
    },
  },
};
</script>

<style>
page {
  background: #fff;
}
</style>
